#define LUA_LIB
  
#include "lprefix.h"

#ifdef _3DS
  #include <3ds.h> 
#endif
#include <stdio.h> 
  
#include "lua.h"
 
#include "lauxlib.h"
#include "lualib.h"

static int console_hello(lua_State *L)
{
#ifdef _3DS
  printf("\x1b[15;19HHello LUA :)!");
#else
  printf("Hello World!\n");
#endif

  return 0;
}

static int console_puts(lua_State *L)
{
    int argc = lua_gettop(L);

    if( (argc!=1) && (argc!=3) ) {
        return luaL_error(L, "wrong number of arguments");
    }

    char* text = (char*)(luaL_checkstring(L, 1));
    if(argc > 1) {
        int x = luaL_checkinteger(L, 2);
        int y = luaL_checkinteger(L, 3);
        printf("\x1b[%d;%dH",y,x);
    }

    printf(text);

    return 0;
}

static int console_init(lua_State *L)
{
  return 0;
}

static const luaL_Reg consolelib[] = {
  {"init", console_init},
  {"hello", console_hello},
  {"puts", console_puts},
  {NULL,  NULL},
};

LUAMOD_API int luaopen_console (lua_State *L) {
  luaL_newlib(L, consolelib);

  return 1;
} 

